#include "music.h"

_music_play music_play;

uint8_t  music_buf[256];
uint32_t buff[50];

uint8_t music_num[4] ={0};
uint8_t name_number[40] = {0};
uint8_t name_name[50][60]= {0};

uint8_t sound = 0;
uint8_t sound_state = 0;//音量是否改变过
uint8_t bl_state = 0; //蓝牙连接状态
uint8_t bt401_ready = 0; //BT401已准备好
uint8_t silence_flag = 0;//是否静音
uint8_t silence_state = 0;//静音状态

uint8_t last_state = 0;//上一曲状态
uint8_t next_state = 0;//下一曲状态
uint8_t play_state = 0;//播放暂停状态
uint8_t play_file = 0;//播放的文件夹

uint8_t ok_state = 0;//指令发送成功
/***********************************************
*函数名称：music_init
*功    能：BT401初始化
*入口参数：无
*返 回 值：无	
*备    注：
************************************************/  
void BT401_init(void)
{
	uart1_Init(115200);                                                           //初始化uart1
	BT401_Send_CMD("AT+CM03",0xff,0x08);//SD卡音乐播放
	BT401_Send_CMD("AT+AF/01*/*.???",0xff,0x01);//播放第一个文件夹
	BT401_Send_CMD("AT+CM03",0xff,0x08);//SD卡音乐播放
	BT401_Send_CMD("AT+AA00",0xff,0x01);//停止播放
	BT401_Send_CMD("AT+CA00",0xff,0x01);//声音为0
	lv_task_handler();
	get_music_list_info("AT+AF/01*/*.???",0);
	get_music_list_info("AT+AF/02*/*.???",10);
	get_music_list_info("AT+AF/03*/*.???",20);
	get_music_list_info("AT+AF/04*/*.???",30);

	BT401_Send_CMD("AT+CA03",0xff,0x01);//声音为0
	BT401_Send_CMD("AT+AA00",0xff,0x01);//停止播放
//	BT401_Send_CMD("AT+CA00",0xff,0x01);//声音为0
}


uint8_t BT401_Read(void)
{
	uint8_t answer_ok = 0;//指令发送成功
	uint8_t len = 0;
	uint8_t i,m;
	uint8_t music_len = 0;
	uart1_Receive_Data(music_buf,&len);
	
	if(len > 0){
		for(i = 0;i < len - 3;i ++){
			if(music_buf[i] == 'O' && music_buf[i + 1] == 'K'){
				answer_ok |= 0x01;
			}else if(music_buf[i] == 'M' && music_buf[i + 1] == '1'){                 //播放文件序号
				answer_ok |= 0x02;
				music_play.play_num = M1_analysis(music_buf + 3 + i);
			}else if(music_buf[i] == 'M' && music_buf[i + 1] == '2'){                 //播放文件总数
				music_play.play_file = M1_analysis(music_buf + 3 + i);
				i += 8;
			}else if(music_buf[i] == 'M' && music_buf[i + 1] == 'T'){                 //播放文件总时长
				music_play.play_time = M1_analysis(music_buf + 3 + i);
				i += 8;
			}else if(music_buf[i] == 'M' && music_buf[i + 1] == 'K'){                 //歌曲已播放时间
				music_play.play_now = M1_analysis(music_buf + 3 + i);
				i += 8;
			}else if(music_buf[i] == 'M' && music_buf[i + 1] == 'F'){                 //歌曲名
				for(m = i;m < len;m += 2){
					if(music_buf[3 + m] == 0x0D){
						break;
					}else{
						if(music_buf[3 + m] < 0x7F && music_buf[4 + m] == 0x00){
							buff[music_len++] = music_buf[3 + m];
						}else{
							buff[music_len] = music_buf[3 + m];
							buff[music_len] |= music_buf[4 + m] << 8;
							music_len++;
							if(music_len > (sizeof(buff)-1)){break;}
						}
					}
				}
				i = m;
				memset(music_play.music_name,0x00,100);
				music_play.name_len = enc_unicode_to_utf8_one(buff,(uint8_t *)music_play.music_name,music_len);
				bt401_ready = 1;
				answer_ok |= 0x04;//读取到歌曲名
			}else if(music_buf[i] == 'T' && music_buf[i + 1] == 'S'){                 //蓝牙连接状态
				if(music_buf[i + 4] == '0'){                                            //蓝牙未连接，等待匹配
					bl_state = 0;
				}else if(music_buf[i + 4] == '1'){                                      //蓝牙连接成功，但是没有播放音乐
					bl_state = 1;
				}else if(music_buf[i + 4] == '2'){                                      //蓝牙正在播放音乐
					bl_state = 2;
				}
				bt401_ready = 1;
			}else if(music_buf[i] == 'M' && music_buf[i + 1] == 'U'){
				if(music_buf[i + 3] == '0'){
					answer_ok |= 0x08;
				}
				
			}
		}
		memset(music_buf,0x00,256);
	}
	return answer_ok;
}

/***********************************************
*函数名称：my_pow
*功    能：求幂
*入口参数：num  底数
           pow  指数
*返 回 值：num
*备    注：
************************************************/
static uint32_t my_pow(uint32_t num,uint8_t pow)
{
	uint8_t i;
	uint32_t num1 = num;
	if(pow == 0) return 1;
	else{
		for(i = 1;i < pow;i++){
			num1 *= num;
		}
	}
	
	return num1;
}


/***********************************************
*函数名称：M1_analysis
*功    能：芯片开始播放信息解码
*入口参数：无
*返 回 值：无
*备    注：
************************************************/ 

static uint16_t M1_analysis(uint8_t *p)
{
	uint8_t i;
	uint8_t pos = 0;
	uint32_t pow = 0;
	uint32_t result = 0;
	
	for(i = 0;i < 8;i++){
		if(*p != '0'){
			pow = my_pow(16,7 - i);
			if(*p <= '9'){
				pos = *p - '0';
				
			}else{
				pos = *p - 'A' + 10;
				
			}
			result += (pos * pow);
		}
		p++;
	}
		
	return result;
}



/***********************************************
*函数名称：music_player
*功    能：播放器显示
*入口参数：无
*返 回 值：无
*备    注：
************************************************/ 	
void music_player(void)
{
	static int time_flag = 0;
	static int bl_flag = 0;
	char t_buf[12] = {0};
	
	int16_t percent;
	if(tab_view0[0] ==NULL || tab_view0[4] ==NULL || tab_view0[5] ==NULL){return;}
	if(music_play.play_flag & 0x01 && bl_state == 0){
	sprintf(t_buf,"%02d:%02d/%02d:%02d",music_play.play_now/60,music_play.play_now%60,music_play.play_time/60,music_play.play_time%60);
	}
	if(time_flag != music_play.play_now && music_play.play_time != 0)
	{
		bl_flag = 0;
		time_flag = music_play.play_now;
		percent = music_play.play_now *100 / music_play.play_time;
		lv_label_set_array_text(tab_view0[5],t_buf,11);
		lv_bar_set_value(tab_view0[4],percent,LV_ANIM_OFF);
	}
}



static uint8_t enc_unicode_to_utf8_one(uint32_t *pbuff, uint8_t *pOutput,uint8_t pbuff_len)
{
	uint8_t i;
	uint8_t name_len = 0;
	uint32_t unic;
	for(i=0;i<pbuff_len;i++){
		unic = pbuff[i];
		if ( unic <= 0x0000007F )
		{
				// * U-00000000 - U-0000007F:  0xxxxxxx
				*pOutput     = (unic & 0x7F);
			pOutput++;
			name_len++;
		}
		else if ( unic >= 0x00000080 && unic <= 0x000007FF )
		{
				// * U-00000080 - U-000007FF:  110xxxxx 10xxxxxx
				*(pOutput+1) = (unic & 0x3F) | 0x80;
				*pOutput     = ((unic >> 6) & 0x1F) | 0xC0;
			pOutput += 2;
			name_len += 2;
		}
		else if ( unic >= 0x00000800 && unic <= 0x0000FFFF )
		{
				// * U-00000800 - U-0000FFFF:  1110xxxx 10xxxxxx 10xxxxxx
				*(pOutput+2) = (unic & 0x3F) | 0x80;
				*(pOutput+1) = ((unic >>  6) & 0x3F) | 0x80;
				*pOutput     = ((unic >> 12) & 0x0F) | 0xE0;
			pOutput += 3;
			name_len += 3;
		}
		else if ( unic >= 0x00010000 && unic <= 0x001FFFFF )
		{
				// * U-00010000 - U-001FFFFF:  11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
				*(pOutput+3) = (unic & 0x3F) | 0x80;
				*(pOutput+2) = ((unic >>  6) & 0x3F) | 0x80;
				*(pOutput+1) = ((unic >> 12) & 0x3F) | 0x80;
				*pOutput     = ((unic >> 18) & 0x07) | 0xF0;
			pOutput += 4;
			name_len += 4;
		}
		else if ( unic >= 0x00200000 && unic <= 0x03FFFFFF )
		{
				// * U-00200000 - U-03FFFFFF:  111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
				*(pOutput+4) = (unic & 0x3F) | 0x80;
				*(pOutput+3) = ((unic >>  6) & 0x3F) | 0x80;
				*(pOutput+2) = ((unic >> 12) & 0x3F) | 0x80;
				*(pOutput+1) = ((unic >> 18) & 0x3F) | 0x80;
				*pOutput     = ((unic >> 24) & 0x03) | 0xF8;
			pOutput += 5;
			name_len += 5;
		}
		else if ( unic >= 0x04000000 && unic <= 0x7FFFFFFF )
		{
				// * U-04000000 - U-7FFFFFFF:  1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
				*(pOutput+5) = (unic & 0x3F) | 0x80;
				*(pOutput+4) = ((unic >>  6) & 0x3F) | 0x80;
				*(pOutput+3) = ((unic >> 12) & 0x3F) | 0x80;
				*(pOutput+2) = ((unic >> 18) & 0x3F) | 0x80;
				*(pOutput+1) = ((unic >> 24) & 0x3F) | 0x80;
				*pOutput     = ((unic >> 30) & 0x01) | 0xFC;
			pOutput += 6;
			name_len += 6;
		}
	}
	return name_len;
}


/***********************************************
*函数名称：tabview0_btn_event_handler
*功    能：图片按钮回调函数
*入口参数：sound     设置音量
*返 回 值：无
*备    注：
************************************************/ 
void Set_Sound(uint8_t sound)
{
	switch(sound)
	{
		case 0:uart1_Send_Data("AT+CA00\r\n");break;
		case 1:uart1_Send_Data("AT+CA03\r\n");break;
		case 2:uart1_Send_Data("AT+CA06\r\n");break;
		case 3:uart1_Send_Data("AT+CA09\r\n");break;
		case 4:uart1_Send_Data("AT+CA12\r\n");break;
		case 5:uart1_Send_Data("AT+CA15\r\n");break;
		case 6:uart1_Send_Data("AT+CA18\r\n");break;
		case 7:uart1_Send_Data("AT+CA21\r\n");break;
		case 8:uart1_Send_Data("AT+CA24\r\n");break;
		case 9:uart1_Send_Data("AT+CA27\r\n");break;
		case 10:uart1_Send_Data("AT+CA30\r\n");break;
	}
}

/***********************************************
*函数名称：music_btn_return
*功    能：蓝牙状态返回控制
*入口参数：无
*返 回 值：无
*备    注：
************************************************/ 
void music_btn_return(void)
{
	static uint8_t bl_st = 0xff;
	uint8_t silence_sound;
	char music_sound[10] = {0};
	
	if(bt401_ready == 1){
		if(bl_st != bl_state){
			if(bl_st == 0xff){
				bl_st = bl_state;
				return;
			}
			bl_st = bl_state;
//			if(tab_view0[0] == NULL || tab_view0[7] == NULL){return;}
			if(bl_st == 0){
				music_play.play_flag = 0;
//				BT401_Send_CMD("AT+CM03",0xff,5);//SD卡音乐播放
//				BT401_Send_CMD("AT+AA00",0xff,1);//停止播放
//				BT401_Send_CMD("AT+CU01",0xff,1);//静音
//				lv_imgbtn_set_state(tab_view0[7],LV_BTN_STATE_REL);
//				lv_label_set_array_text(tab_view0[0],music_play.music_name,music_play.name_len);
			}
			else if(bl_st == 1){
				lv_imgbtn_set_state(tab_view0[7],LV_BTN_STATE_REL);
			}else if(bl_st == 2){
				lv_imgbtn_set_state(tab_view0[7],LV_BTN_STATE_TGL_REL);
			}
		}
	}
	if(music_play.name_len != 0){        //显示歌曲名
		if(tab_view0[0] == NULL || tab_view0[5] == NULL){return;}
		lv_label_set_array_text(tab_view0[0],music_play.music_name,music_play.name_len);
		lv_label_set_text(tab_view0[5],"00:00/00:00");
		music_play.name_len = 0;
	}
	if(sound_state != 0){                                                         //修改音量
		sound_state --;
		if(tab_view0[2] == NULL || tab_view0[3] == NULL){return;}
		sprintf(music_sound,"%d",sound);
		lv_label_set_array_text(tab_view0[3], music_sound,2);
		Set_Sound(sound);
		if(sound == 0){
			silence_flag = 1;
			lv_imgbtn_set_state(tab_view0[2],LV_BTN_STATE_TGL_REL);
		}else {
			lv_imgbtn_set_state(tab_view0[2],LV_BTN_STATE_REL);
			silence_flag = 0;
		}
	}
	if(silence_state != 0){                                                       //静音切换
		silence_sound = sound;
		silence_state--;
		if(tab_view0[1] ==NULL || tab_view0[3] ==NULL){return;}
		if(silence_flag == 0){
			silence_flag = 1;
			uart1_Send_Data("AT+CU01\r\n");//静音
			lv_slider_set_value(tab_view0[1],0,LV_ANIM_OFF);
			lv_label_set_text(tab_view0[3],"0");
		}else {
			silence_flag = 0;
			uart1_Send_Data("AT+CU00\r\n");//解除静音
			lv_slider_set_value(tab_view0[1],silence_sound,LV_ANIM_OFF);
			sprintf(music_sound,"%d",silence_sound);
			lv_label_set_array_text(tab_view0[3],music_sound,2);
		}
		
	}
	if(last_state != 0){                                                          //上一曲
		last_state--;
		if(tab_view0[7] == NULL){return;}
		uart1_Send_Data("AT+CD\r\n");//播放上一曲
		music_play.play_flag = 1;
		lv_imgbtn_set_state(tab_view0[7],LV_BTN_STATE_TGL_REL);
	}
	if(next_state != 0){                                                          //下一曲
		next_state--;
		if(tab_view0[7] == NULL){return;}
		uart1_Send_Data("AT+CC\r\n");//播放下一曲
		lv_imgbtn_set_state(tab_view0[7],LV_BTN_STATE_TGL_REL);
		music_play.play_flag = 1;
	}
	if(play_state != 0){                                                          //播放\暂停切换
		play_state--;
		if(bl_state == 0){
			if(play_file == 0x01){
				uart1_Send_Data("AT+CM03\r\n");//设置TF卡播放
			}else if(play_file == 0x10){
				uart1_Send_Data("AT+AF/01*/*.???\r\n");//指定01开头的文件夹循环播放
			}else if(play_file == 0x20){
				uart1_Send_Data("AT+AF/02*/*.???\r\n");//指定02开头的文件夹循环播放
			}else if(play_file == 0x07){
				uart1_Send_Data("AT+AF/07*/*.???\r\n");//指定07开头的文件夹循环播放
			}
		}
		if(music_play.play_flag == 0){
			music_play.play_flag =1;
			if(bl_state != 0){
				uart1_Send_Data("AT+CB\r\n");//蓝牙暂停/开始
			}else if(bl_state == 0 && play_file == 0){
				uart1_Send_Data("AT+AA01\r\n");//SD卡开始
			}
		}
		else{
			music_play.play_flag = 0;
			if(bl_state != 0){
				uart1_Send_Data("AT+CB\r\n");//蓝牙暂停/开始
			}else if(bl_state == 0 && play_file == 0){
				uart1_Send_Data("AT+AA02\r\n");//SD卡暂停
			}
		}
		play_file = 0;
	}
}


